package com.hnevc.asyncokhttp3demo;

import android.content.Intent;
import android.os.AsyncTask;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ProgressBar;
import android.widget.Toast;

import org.json.JSONException;
import org.json.JSONObject;

import java.io.IOException;

import okhttp3.Headers;
import okhttp3.MediaType;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.RequestBody;
import okhttp3.Response;

public class MainActivity extends AppCompatActivity {

    EditText mEtUserName;
    EditText mEtUserPassword;
    Button mBtnLogin;
    Button mBtnShowFoods;
    ProgressBar mPb;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        initView();
        mBtnLogin.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                String username = mEtUserName.getText().toString();
                String password = mEtUserPassword.getText().toString();

                MyTask myTask = new MyTask();
                myTask.execute("http://"+getResources().getString(R.string.ServerIp)+":8080/RestaurantServer/user/doLogin"
                        ,username
                        ,password);
            }
        });
        mBtnShowFoods.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                //TODO 编写点击事件的处理方法。
                Intent intent = new Intent();
                intent.setClass(MainActivity.this,ShowFoodActivity.class);
                startActivity(intent);
            }
        });
    }

    private void initView() {
        mBtnShowFoods = (Button) findViewById(R.id.id_btn_show_foods);
        mBtnLogin = (Button) findViewById(R.id.id_btn_login);
        mEtUserName = (EditText) findViewById(R.id.id_tv_user_name);
        mEtUserPassword = (EditText) findViewById(R.id.id_et_user_password);
    }

    public class MyTask extends AsyncTask<String,Integer,String> {
        @Override
        protected void onPreExecute() {

        }

        @Override
        protected void onPostExecute(String s) {
            Toast.makeText(MainActivity.this, s, Toast.LENGTH_SHORT).show();


        }

        @Override
        protected void onProgressUpdate(Integer... values) {
            mPb.setProgress(values[0]);
        }



        @Override
        protected String doInBackground(String... strings) {
            String data="";
            String url = strings[0];
            OkHttpClient client = new OkHttpClient.Builder()
                    .cookieJar(new MyCookieJar(MainActivity.this))
                    .build();
            JSONObject object = new JSONObject();
            try {
                object.put("userName",strings[1]);
                object.put("userPassword",strings[2]);
            } catch (JSONException e) {
                e.printStackTrace();
            }

            RequestBody body = RequestBody.create(MediaType.parse("application/json"),object.toString());
            /*RequestBody body = new FormBody.Builder()
                    .add("userName","tom")
                    .add("userPassword","123")
                    .build();*/
            Request request = new Request.Builder()
                    .url(url)
                    .post(body)
                    .build();
            try {
                Response response = client.newCall(request).execute();


                data = response.body().string();

            } catch (IOException e) {
                e.printStackTrace();
            }
            return data;
        }
    }



}
